package com.gzedu.module.faced.curd.impl;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gzedu.common.db.DBResult;
import com.gzedu.common.util.page.JqPage;
import com.gzedu.common.util.page.Page;
import com.gzedu.module.faced.curd.CommonQueryFacade;
import com.gzedu.module.modle.curd.CurdButton;
import com.gzedu.module.modle.curd.CurdForm;
import com.gzedu.module.modle.curd.CurdJs;
import com.gzedu.module.modle.curd.CurdList;
import com.gzedu.module.service.curd.ICommonQueryService;
import com.gzedu.module.service.curd.ICurdButtonService;
import com.gzedu.module.service.curd.ICurdFormService;
import com.gzedu.module.service.curd.ICurdJsService;
import com.gzedu.module.service.curd.ICurdListService;
import com.gzedu.module.utils.IdGen;

import net.sf.json.JSONArray;

@Service
public class CommonQueryFacadeImpl implements CommonQueryFacade{

	@Autowired
	private ICommonQueryService commonQueryService;
	@Autowired
	private ICurdFormService curdFormService;
	@Autowired
	private ICurdListService curdListService;
	@Autowired
	private ICurdButtonService curdButtonService;
	@Autowired
	private ICurdJsService curdJsService;

	public JqPage dataQuery(String sql,String orderBy, Page page) {
		// TODO Auto-generated method stub
		int dataCount = DBResult.findCount(sql);
		List<Map<String,Object>> res = DBResult.getPageRes(sql, orderBy, page);
		page.setTotalRecord(dataCount);
		
		JqPage jp = new JqPage();
		jp.setPage(page.getPageNo());
		jp.setRecords(dataCount);
		jp.setTotal(page.getTotalPage());
		jp.setRows(res);
		
		return jp;
	}

	public Map<String,Object> insertCommonQuery(HttpServletRequest request,CurdForm curdForm) {
		// TODO Auto-generated method stub
		Map<String,Object> res = new HashMap<String,Object>();
		Map<String, String[]> map = request.getParameterMap();
		if(null != map){
			Iterator<Map.Entry<String, String[]>> it = map.entrySet().iterator();
			while(it.hasNext()){
				Map.Entry<String, String[]> entry = it.next();
				
				System.out.println("key:"+entry.getKey());
				
				if(entry.getKey().toString().trim().indexOf("btn") != -1){
					String[] btnVal = entry.getValue();
					String btnStr = btnVal[0];
					String[] btnSplit = btnStr.split(",");
					try {
						CurdButton curdButton = new CurdButton();
						curdButton.setId(IdGen.creatUUID());
						curdButton.setButton_id(btnSplit[0].trim());
						curdButton.setButton_name(btnSplit[1]);
						curdButton.setCurd_form_id(curdForm.getCurd_form_id());
						curdButton.setOnclick(btnSplit[2].trim());
						curdButton.setCreate_dt(new Date());
						curdButtonService.insert(curdButton);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						res.put("flag", false);
						e.printStackTrace();
					}
					
				}else if(entry.getKey().toString().trim().indexOf("tabSpan") != -1){
					String[] tabVal = entry.getValue();
					String tabValStr = tabVal[0];
					String[] tabSplit = tabValStr.split(",");
					try{
						CurdList curdList = new CurdList();
						curdList.setId(IdGen.creatUUID());
						curdList.setCurd_form_id(curdForm.getCurd_form_id());
						
						curdList.setCurd_list_id(tabSplit[0]);
						curdList.setList_column(tabSplit[2]);
						curdList.setFiled_name(tabSplit[1]);
						curdList.setPosition(tabSplit[3]);
						curdList.setCreate_date(new Date());
						
						curdListService.insert(curdList);
					}catch(Exception e){
						res.put("flag", false);
						e.printStackTrace();
					}
					
				}else if("js".equals(entry.getKey())){
					CurdJs curdJs = new CurdJs();
					curdJs.setId(IdGen.creatUUID());
					curdJs.setCurd_form_id(curdForm.getCurd_form_id());
					curdJs.setJs_content(getStr(entry.getValue()));
					curdJsService.insert(curdJs);
				}else{
					
					if("curd_form_id".equals(entry.getKey())){
						curdForm.setCurd_form_id(getStr(entry.getValue()));
					}else if("order_by".equals(entry.getKey())){
						curdForm.setOrder_by(getStr(entry.getValue()));
					}else if("table_id_col".equals(entry.getKey())){
						curdForm.setTable_id_col(getStr(entry.getValue()));
					}else if("sql_content".equals(entry.getKey())){
						curdForm.setSql_content(getStr(entry.getValue()));
					}else if("module_path".equals(entry.getKey())){
						curdForm.setModule_path(getStr(entry.getValue()));
					}
					
				}
			}
			curdForm.setId(IdGen.creatUUID());
			curdFormService.insert(curdForm);
			res.put("flag", true);
			
		}
		return res;
	}
	
	public Map<String,Object> update(HttpServletRequest request){
		Map<String,Object> res = new HashMap<String,Object>();
		CurdForm curdForm = new CurdForm();
		Map<String, String[]> map = request.getParameterMap();
		if(null != map){
			Iterator<Map.Entry<String, String[]>> it = map.entrySet().iterator();
			
			while(it.hasNext()){
				Map.Entry<String, String[]> entry = it.next();
				
				System.out.println("key:"+entry.getKey());
				
				
				if(entry.getKey().toString().trim().indexOf("btn") != -1){
					String[] btnVal = entry.getValue();
					String btnStr = btnVal[0];
					String[] btnSplit = btnStr.split(",");
					try {
						CurdButton curdButton = new CurdButton();
						curdButton.setId(IdGen.creatUUID());
						curdButton.setButton_id(btnSplit[0].trim());
						curdButton.setButton_name(btnSplit[1]);
						curdButton.setCurd_form_id(curdForm.getCurd_form_id());
						curdButton.setOnclick(btnSplit[2].trim());
						curdButton.setCreate_dt(new Date());
						
						
						curdButtonService.insert(curdButton);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						res.put("flag", false);
						e.printStackTrace();
					}
					
				}else if(entry.getKey().toString().trim().indexOf("tabSpan") != -1){
					String[] tabVal = entry.getValue();
					String tabValStr = tabVal[0];
					String[] tabSplit = tabValStr.split(",");
					try{
						CurdList curdList = new CurdList();
						curdList.setId(IdGen.creatUUID());
						curdList.setCurd_form_id(curdForm.getCurd_form_id());
						
						curdList.setCurd_list_id(tabSplit[0]);
						curdList.setList_column(tabSplit[2]);
						curdList.setFiled_name(tabSplit[1]);
						curdList.setPosition(tabSplit[3]);
						curdList.setCreate_date(new Date());
						
						
						curdListService.insert(curdList);
					}catch(Exception e){
						res.put("flag", false);
						e.printStackTrace();
					}
					
				}else if("js".equals(entry.getKey())){
					CurdJs curdJs = new CurdJs();
					curdJs.setId(IdGen.creatUUID());
					curdJs.setCurd_form_id(curdForm.getCurd_form_id());
					curdJs.setJs_content(getStr(entry.getValue()));
					
					curdJsService.insert(curdJs);
				}else{
					
					if("curd_form_id".equals(entry.getKey())){
						curdForm.setCurd_form_id(getStr(entry.getValue()));
						
						//删除重复的数据
						CurdButton curdButton = new CurdButton();
						curdButton.setCurd_form_id(curdForm.getCurd_form_id());
						List<CurdButton> curdBtnList = curdButtonService.selectByFormId(curdButton);
						if(curdBtnList != null && curdBtnList.size() > 0){
							curdButtonService.deleteByFormId(curdButton);
						}
						
						CurdList curdList = new CurdList();
						curdList.setCurd_form_id(curdForm.getCurd_form_id());
						List<CurdList> curdLists = curdListService.getByFormId(curdList);
						if(curdLists != null && curdLists.size() > 0){
							curdListService.deleteByFormId(curdList);
						}
						
						CurdJs curdJs = new CurdJs();
						curdJs.setCurd_form_id(curdForm.getCurd_form_id());
						List<CurdJs> curdJsList = curdJsService.selectByFormId(curdJs);
						if(curdJsList != null && curdJsList.size() > 0){
							curdJsService.deleteByFormId(curdJs);
						}
					}else if("order_by".equals(entry.getKey())){
						curdForm.setOrder_by(getStr(entry.getValue()));
					}else if("table_id_col".equals(entry.getKey())){
						curdForm.setTable_id_col(getStr(entry.getValue()));
					}else if("sql_content".equals(entry.getKey())){
						curdForm.setSql_content(getStr(entry.getValue()));
					}else if("id".equals(entry.getKey())){
						curdForm.setId(getStr(entry.getValue()));
					}else if("module_path".equals(entry.getKey())){
						curdForm.setModule_path(getStr(entry.getValue()));
					}
					
				}
			}
			curdFormService.updateById(curdForm);
			res.put("flag", true);
			
		}
		return res;
	}
	
	private String getStr(String[] value){
		return value[0];
	}

}
